package com.bdqn.t330.ch12.util;


import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class String2DateConverter implements Converter<String, Date> {

    //定义日期变量
    private Logger logger = Logger.getLogger(String2DateConverter.class);

    private String datePattern;

    //通过构造注入的方式进行赋值
    public String2DateConverter(String datePattern) {
        this.datePattern = datePattern;
    }

    @Override
    public Date convert(String s) {
        Date date = null;
        try {
            date = new SimpleDateFormat(datePattern).parse(s);
        } catch (ParseException e) {
            logger.error("日期转换异常");
            e.printStackTrace();
        }
        return date;
    }
}
